using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;

public class SaveGameController : MonoBehaviour
{
	public static SaveGameController instance;

	private List<int> listLevelUpgrade = new List<int>();

	private void Awake()
	{
		if (instance != null && instance != this)
		{
			UnityEngine.Object.Destroy(base.gameObject);
			return;
		}
		instance = this;
		Object.DontDestroyOnLoad(base.gameObject);
		if (!ES2.Exists("key_sound"))
		{
			ES2.Save(GameManager.key_sound, "key_sound");
		}
		else
		{
			GameManager.key_sound = ES2.Load<int>("key_sound");
		}
		if (!ES2.Exists("key_music"))
		{
			ES2.Save(GameManager.key_music, "key_music");
		}
		else
		{
			GameManager.key_music = ES2.Load<int>("key_music");
		}
		if (!ES2.Exists("LevelPlayer"))
		{
			ES2.Save(GameManager.levelPlayer, "LevelPlayer");
		}
		else
		{
			GameManager.levelPlayer = ES2.Load<int>("LevelPlayer");
		}
		if (!ES2.Exists("ExpEnemyDie"))
		{
			ES2.Save(GameManager.ExpEnemyDie, "ExpEnemyDie");
		}
		else
		{
			GameManager.ExpEnemyDie = ES2.Load<int>("ExpEnemyDie");
		}
		if (!ES2.Exists("levelUpgrade"))
		{
			ES2.Save(GameManager.levelUpgrade, "levelUpgrade");
		}
		else
		{
			GameManager.levelUpgrade = ES2.LoadArray<int>("levelUpgrade");
			if (GameManager.levelUpgrade.Length < 10)
			{
				listLevelUpgrade = GameManager.levelUpgrade.ToList();
				UnityEngine.Debug.Log(listLevelUpgrade.Count);
				for (int i = 0; i < 3; i++)
				{
					listLevelUpgrade.Add(-1);
				}
				GameManager.levelUpgrade = listLevelUpgrade.ToArray();
			}
		}
		if (!ES2.Exists("key_stage"))
		{
			ES2.Save(GameManager.key_stage, "key_stage");
		}
		else
		{
			GameManager.key_stage = ES2.Load<int>("key_stage");
		}
		if (!ES2.Exists("key_stageHard"))
		{
			ES2.Save(GameManager.key_stageHard, "key_stageHard");
		}
		else
		{
			GameManager.key_stageHard = ES2.Load<int>("key_stageHard");
		}
		if (!ES2.Exists("Tutorial"))
		{
			ES2.Save(GameManager.Tutorial, "Tutorial");
		}
		else
		{
			GameManager.Tutorial = ES2.Load<int>("Tutorial");
		}
		if (!ES2.Exists("BonusStage"))
		{
			ES2.Save(GameManager.BonusStage, "BonusStage");
		}
		else
		{
			GameManager.BonusStage = ES2.LoadArray<int>("BonusStage");
		}
		if (!ES2.Exists("BonusStageHard"))
		{
			ES2.Save(GameManager.BonusStageHard, "BonusStageHard");
		}
		else
		{
			GameManager.BonusStageHard = ES2.LoadArray<int>("BonusStageHard");
		}
		if (!ES2.Exists("numberHealth"))
		{
			ES2.Save(GameManager.numberHealth, "numberHealth");
		}
		else
		{
			GameManager.numberHealth = ES2.Load<int>("numberHealth");
		}
		if (!ES2.Exists("numberBigHealth"))
		{
			ES2.Save(GameManager.numberBigHealth, "numberBigHealth");
		}
		else
		{
			GameManager.numberBigHealth = ES2.Load<int>("numberBigHealth");
		}
		if (!ES2.Exists("TotalCoin"))
		{
			ES2.Save(GameManager.TotalCoin, "TotalCoin");
		}
		else
		{
			GameManager.TotalCoin = ES2.Load<int>("TotalCoin");
		}
		if (!ES2.Exists("indexVukhi"))
		{
			ES2.Save(GameManager.indexVukhi, "indexVukhi");
		}
		else
		{
			GameManager.indexVukhi = ES2.Load<int>("indexVukhi");
		}
		if (!ES2.Exists("Dog"))
		{
			ES2.Save(GameManager.Dog, "Dog");
		}
		else
		{
			GameManager.Dog = ES2.Load<int>("Dog");
		}
		if (!ES2.Exists("Tinker"))
		{
			ES2.Save(GameManager.Tinker, "Tinker");
		}
		else
		{
			GameManager.Tinker = ES2.Load<int>("Tinker");
		}
		if (!ES2.Exists("rate"))
		{
			ES2.Save(GameManager.rate, "rate");
		}
		else
		{
			GameManager.rate = ES2.Load<int>("rate");
		}
		if (!ES2.Exists("numberRate"))
		{
			ES2.Save(GameManager.numberRate, "numberRate");
		}
		else
		{
			GameManager.numberRate = ES2.Load<int>("numberRate");
		}
		if (!ES2.Exists("Key_numberkill"))
		{
			ES2.Save(GameManager.Key_numberkill, "Key_numberkill");
		}
		else
		{
			GameManager.Key_numberkill = ES2.Load<int>("Key_numberkill");
		}
	}

	private void OnApplicationPause(bool statusPause)
	{
		if (statusPause && SceneManager.GetActiveScene().name != "LoadingSpash" && SceneManager.GetActiveScene().name != "Menu")
		{
			SaveGame();
		}
	}

	private void OnApplicationQuit()
	{
		SaveGame();
	}

	public static void SaveGame()
	{
		ES2.Save(GameManager.key_sound, "key_sound");
		ES2.Save(GameManager.key_music, "key_music");
		ES2.Save(GameManager.levelUpgrade, "levelUpgrade");
		ES2.Save(GameManager.levelPlayer, "LevelPlayer");
		ES2.Save(GameManager.ExpEnemyDie, "ExpEnemyDie");
		ES2.Save(GameManager.key_stage, "key_stage");
		ES2.Save(GameManager.key_stageHard, "key_stageHard");
		ES2.Save(GameManager.Tutorial, "Tutorial");
		ES2.Save(GameManager.BonusStage, "BonusStage");
		ES2.Save(GameManager.BonusStageHard, "BonusStageHard");
		ES2.Save(GameManager.TotalCoin, "TotalCoin");
		ES2.Save(GameManager.numberHealth, "numberHealth");
		ES2.Save(GameManager.numberBigHealth, "numberBigHealth");
		ES2.Save(GameManager.indexVukhi, "indexVukhi");
		ES2.Save(GameManager.Dog, "Dog");
		ES2.Save(GameManager.Tinker, "Tinker");
		ES2.Save(GameManager.rate, "rate");
		ES2.Save(GameManager.numberRate, "numberRate");
		ES2.Save(GameManager.Key_numberkill, "Key_numberkill");
	}
}
